# -*- coding: utf-8 -*-
"""
# 使用了 [numpy==1.24.4]，遵循其 [BSD-3-Clause] 许可证，原始代码来源：[https://www.numpy.org]
# 使用了 [pandas==2.0.3]，遵循其 [BSD 3-Clause License] 许可证，原始代码来源：[https://pandas.pydata.org]
# 使用了 [scikit-learn==1.3.2]，遵循其 [new BSD] 许可证，原始代码来源：[http://scikit-learn.org]
"""
import sys
from pathlib import Path
import numpy as np
import pandas as pd
from sklearn.decomposition import LatentDirichletAllocation as LDA

class PARA_CONFIG:
    CURRENT_DIR = Path(__file__).parent.resolve()
    TFIDF_PATH=CURRENT_DIR.parent.parent.resolve()/"01_prepare_data"/"src"/"result"/"1_ICT_TFIDF_make_20251031_111556.xlsx"
    FUNC_PATH=CURRENT_DIR.parent.parent.resolve()/"about_file"
sys.path.append(str(PARA_CONFIG.FUNC_PATH))
import f_basic


@f_basic.Timer
def do_lda_analyse(fp,n_topics=0):
    (titles,cv,li_keys)=f_basic.make_csr_matrix(fp)#make csr.
    cnt_paper=len(titles)
    if n_topics==0:
        N_TOPICS=f_basic.sqrt_ceil(cnt_paper)
    else:
        N_TOPICS=n_topics
    lda=LDA(n_components=N_TOPICS,max_iter=100,doc_topic_prior=1.0/N_TOPICS,topic_word_prior=0.01,learning_method='batch',random_state=0)#create lda model.
    lda_fit=lda.fit_transform(cv)#fit data.
    df_topic=pd.DataFrame(np.array(lda_fit),columns=[f"{i}" for i in range(N_TOPICS)],dtype=np.float32)
    df_components=pd.DataFrame(np.array(lda.components_),columns=li_keys)
    fp_topics=f_basic.save_dataframe(df_topic,"2_LDA_Topic_Allocation")
    fp_keywords=f_basic.save_dataframe(df_components,"3_LDA_Keyword_Allocation")
    return(fp_topics,fp_keywords)

@f_basic.Timer
def main_stream():
    """SK LDA start."""
    (fp_t,fp_k)=do_lda_analyse(PARA_CONFIG.TFIDF_PATH)
    f_basic.print_topic_keys(fp_k)
    f_basic.draw_tsne_graph(fp_t,prefix="1_LDA")

if __name__=="__main__":
    main_stream()
